package com.sky.controller.user;

import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderAnnounceVo;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrdersDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
@Api(tags = "C端-用户订单接口")
public class OrdersController {


    @Autowired
    OrdersService ordersService;

    @PostMapping("/submit")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        Orders orders = ordersService.submitOrder(ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
                .id(orders.getId())
                .orderTime(orders.getOrderTime())
                .orderNumber(orders.getNumber())
                .orderAmount(orders.getAmount()).build();
        return Result.success(orderSubmitVO);
    }


    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(OrdersPaymentDTO ordersPaymentDTO) {
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 查询历史订单
     * @param queryDTO
     * @return
     */
    @ApiOperation("C端-小程序历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> page(OrdersPageQueryDTO queryDTO){
        PageResult pageResult = ordersService.page(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据订单id取消订单
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancelOrders(@PathVariable("id")Long id) {
        ordersService.cancelOrders(id);

        return Result.success();
    }
    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result detail(@PathVariable("id")Long id){
        OrdersDetailVO ordersDetailVO=ordersService.findOrderDetailById(id);
        return Result.success(ordersDetailVO);
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result again(@PathVariable Long id){
        ordersService.again(id);
        return Result.success();
    }

}
